﻿using JiaBloggingSite.Core;
using JiaBloggingSite.Models;
using Microsoft.Web.WebPages.OAuth;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;

namespace JiaBloggingSite.Controllers
{
    public class BlogController : Controller
    {
        private readonly IBlogRepository _blogRepository;

        public BlogController(IBlogRepository blogRepository)
        {
            _blogRepository = blogRepository;
        }


        public ViewResult Post(int year, int month, string title)
        {
            var post = _blogRepository.Post(year, month, title);

            if (post == null)
                throw new HttpException(404, "Post not found");

            if (post.Published == false && User.Identity.IsAuthenticated == false)
                throw new HttpException(401, "The post is not published");

            return View(post);
        }

        [ChildActionOnly]
        public PartialViewResult SocialNetworkPicture()
        {

            ICollection<OAuthAccount> oAuthUsers = OAuthWebSecurity.GetAccountsFromUserName(User.Identity.Name);

            OAuthAccount loggedInOAuth = null;

            foreach(OAuthAccount OAcc in oAuthUsers)
            {
               //TODO: Need to handle accounts more professionallly. Duplicate Usernames could be a problem over multiple OAuth Providers
                loggedInOAuth = OAcc;
            }


            string profilePictureUrl = "";
            switch (loggedInOAuth.Provider)
            {
                case "facebook":
                    profilePictureUrl = String.Format("https://graph.facebook.com/{0}/picture", loggedInOAuth.ProviderUserId);
                    break;
                case "twitter":
                    profilePictureUrl = String.Format("http://api.twitter.com/1/users/profile_image?size=bigger&user_id={0}", loggedInOAuth.ProviderUserId); 
                    break;
                case "google":
                    profilePictureUrl = "https://plus.google.com/s2/photos/profile/me?sz=50"; 
                    break;
            }

            return PartialView("_SocialNetworkPicture", profilePictureUrl);
        }

        public ViewResult Posts(int p = 1)
        {
            var viewModel = new ListViewModel(_blogRepository, p);

            ViewBag.Title = "Latest Posts";
            return View("List", viewModel);
        }

        public ViewResult Category(string category, int p = 1)
        {
            var viewModel = new ListViewModel(_blogRepository, category, "Category", p);

            if (viewModel.Category == null)
                throw new HttpException(404, "Category not found");

            ViewBag.Title = String.Format(@"Latest posts on category ""{0}""",
                viewModel.Category.Name);
            return View("List", viewModel);
        }

        public ViewResult Tag(string tag, int p = 1)
        {
            var viewModel = new ListViewModel(_blogRepository, tag, "Tag", p);

            if (viewModel.Tag == null)
                throw new HttpException(404, "Tag not found");

            ViewBag.Title = String.Format(@"Latest posts tagged on ""{0}""",
                viewModel.Tag.Name);
            return View("List", viewModel);
        }

        public ViewResult Search(string s, int p = 1)
        {
            ViewBag.Title = String.Format(@"Lists of posts found 
                        for search text ""{0}""", s);

            var viewModel = new ListViewModel(_blogRepository, s, "Search", p);
            return View("List", viewModel);
        }

        [ChildActionOnly]
        public PartialViewResult Sidebars()
        {
            var widgetViewModel = new WidgetViewModel(_blogRepository);
            return PartialView("_Sidebars", widgetViewModel);
        }

    }
}
